package com.work.bookstore.controller;

import com.work.bookstore.mapper.UserMapper;
import com.work.bookstore.pojo.User;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ppg007
 * @date 2020/12/30 17:33
 */
@Controller
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public int login(HttpServletRequest request){
        String account=request.getParameter("account");
        String password=request.getParameter("password");
        User user=userMapper.queryUserByAccount(account);
        if (user==null){
            return -1;
        }
        else {
            if (password.equals(user.getPassword())){
                return 1;
            }
            else {
                return 0;
            }
        }
    }
    @RequestMapping(value = "/register",method =RequestMethod.POST)
    @ResponseBody
    public int register(HttpServletRequest request){
        String account=request.getParameter("account");
        String password=request.getParameter("password");
        if (userMapper.queryUserByAccount(account)==null){
            userMapper.addUser(account,password,"芜湖");
            return 1;
        }
        else {
            return 0;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/changeUserName",method = RequestMethod.POST)
    public String changeUserName(HttpServletRequest request){
        String newUserName=request.getParameter("newUserName");
        String account=request.getParameter("account");
        userMapper.updateUserName(account,newUserName);
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/changeUserPassword",method = RequestMethod.POST)
    public String changeUserPassword(HttpServletRequest request){
        String newUserPassword=request.getParameter("newPassword");
        String account=request.getParameter("account");
        userMapper.updateUserPassword(account,newUserPassword);
        return "success";
    }

    @RequestMapping(value = "/changeUserAddress",method = RequestMethod.POST)
    @ResponseBody
    public String changeUserAddress(HttpServletRequest request){
        String newUserAddress=request.getParameter("newUserAddress");
        String account=request.getParameter("account");
        userMapper.updateUserAddress(account,newUserAddress);
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/getUser",method = RequestMethod.POST)
    public String getUser(HttpServletRequest request){
        String account=request.getParameter("account");
        User user=userMapper.queryUserByAccount(account);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("account",user.getAccount());
        jsonObject.put("id",user.getId());
        jsonObject.put("password",user.getPassword());
        jsonObject.put("score",user.getScore());
        jsonObject.put("username",user.getUsername());
        jsonObject.put("address",user.getAddress());
        return jsonObject.toString();
    }
}
